package com.example.acer.im.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;
import com.example.acer.im.R;
import com.example.acer.im.frags.assist.PermissionsFragment;
import com.example.acer.im.frags.main.ActiveFragment;
import com.example.acer.im.frags.main.ContactFragment;
import com.example.acer.im.frags.main.GroupFragment;
import com.example.acer.im.helper.NacHelper;
import com.example.common.common.app.Activity;
import com.example.common.common.widget.PortraitView;
import com.example.factory.persistence.Account;

import net.qiujuer.genius.ui.Ui;
import net.qiujuer.genius.ui.widget.FloatActionButton;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends Activity implements BottomNavigationView.OnNavigationItemSelectedListener, NacHelper.OnTabChangedListener<Integer> {
    @BindView(R.id.im_portrait)
    PortraitView imPortrait;
    @BindView(R.id.txt_title)
    TextView mTitle;
    @BindView(R.id.im_search)
    ImageView imSearch;
    @BindView(R.id.appbar)
    View mLayAppBar;
    @BindView(R.id.lay_container)
    FrameLayout layContainer;
    @BindView(R.id.btn_action)
    FloatActionButton mAction;
    @BindView(R.id.navigation)
    BottomNavigationView navigation;

    private NacHelper<Integer> helper;

    /**
     * MainActivity 显示的入口
     *
     * @param context 上下文
     */
    public static void show(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }

    @Override
    protected boolean initArges(Bundle bundle) {
        if(Account.isComplete()) {

            //判断用户信息是否完成
            return super.initArges(bundle);
        }else {
            UserActivity.show(this);
            return false;
        }

    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initWidget() {
        super.initWidget();

        PermissionsFragment.haveAll(this,getSupportFragmentManager());
        helper = new NacHelper<>(this, R.id.lay_container,
                getSupportFragmentManager(), this);
        helper.add(R.id.action_home, new NacHelper.Tab<>(ActiveFragment.class, R.string.title_home));
        helper.add(R.id.action_group, new NacHelper.Tab<>(GroupFragment.class, R.string.title_group));
        helper.add(R.id.action_contact, new NacHelper.Tab<>(ContactFragment.class, R.string.title_contact));

        navigation.setOnNavigationItemSelectedListener(this);
        //如果设置为Background的话 那么会被拉伸铺满全部控件
        Glide.with(this).load(R.drawable.bg_src_morning)
                .centerCrop().into(new ViewTarget<View, GlideDrawable>(mLayAppBar) {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                this.view.setBackground(resource.getCurrent());
            }
        });
       PermissionsFragment.haveAll(this,getSupportFragmentManager());

    }

    @Override
    public void initData() {
        super.initData();
        Menu menu = navigation.getMenu();//从底部导航中接管我们的Menu
        menu.performIdentifierAction(R.id.action_home, 0); //触发首次选中home
        // 初始化头像加载
        imPortrait.setup(Glide.with(this), Account.getUser());
    }
    @OnClick(R.id.im_portrait)
    void onPortraitClick() {
        PersonalActivity.show(this, Account.getUserId());
    }

    @OnClick(R.id.im_search)
    void onSearchMenuClick() {
        // 在群的界面的时候，点击顶部的搜索就进入群搜索界面
        // 其他都为人搜索的界面
        int type = Objects.equals(helper.getCurrentTab().extra, R.string.title_group) ?
                SearchActivity.TYPE_GROUP : SearchActivity.TYPE_USER;
        SearchActivity.show(this, type);
    }

    @OnClick(R.id.btn_action)
    void onActionClick() {
        // 浮动按钮点击时，判断当前界面是群还是联系人界面
        // 如果是群，则打开群创建的界面
        if (Objects.equals(helper.getCurrentTab().extra, R.string.title_group)) {
            // 打开群创建界面
//            GroupCreateActivity.show(this);
        } else {
            // 如果是其他，都打开添加用户的界面
            SearchActivity.show(this, SearchActivity.TYPE_USER);
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        return helper.performClickMenu(item);
    }

    @Override
    public void onTabChanged(NacHelper.Tab<Integer> newTab, NacHelper.Tab<Integer> oldTab) {
        mTitle.setText(newTab.extra);
        float transY=0;
        float rotation=0;
        if (Objects.equals(newTab.extra,R.string.title_home)) {
            transY= Ui.dipToPx(getResources(),76);
        }else {
            //群
            if(Objects.equals(newTab.extra,R.string.title_group)) {
                mAction.setImageResource(R.drawable.ic_group_add);
                rotation=-360;
            }else {
                //联系人
                mAction.setImageResource(R.drawable.ic_contact_add);
                rotation=360;
            }

        }
        mAction.animate()
                .rotation(rotation)
                .translationY(transY)
                .setInterpolator(new AnticipateOvershootInterpolator(1))
                .setDuration(480)
                .start();
    }
}
